Ext.ns("sh.mytool.cv.module");

sh.mytool.cv.module.ArticlePanel = Ext.extend(Ext.Panel, {
    layout : {type: 'vbox', align: 'stretch'},
    power : "",
    editUrl : "",
    getUrl : "",
    initComponent : function() {
        sh.mytool.cv.module.ArticlePanel.superclass.initComponent.call(this);
        this.contentPanel = new Ext.Panel({
            flex: 1,
            border : true,
//            width: "100%",
            preventBodyReset : true,
            bodyCssClass : "wiki_content",
            split : true,
            autoScroll : true,
            listeners: {
                "afterrender": function(cmp){
                    var el = cmp.getEl();
                    el.unselectable();
                    // T ?
//                    el.addKeyMap({
//                        key: "c",
//                        scope: el,
//                        fn: function(k, e){
//                            alert(1);
//                        }
//                    });
                    el.on("contextmenu", function(e, t, o){
                        e.stopEvent();
                    }, el);
                    // T ?
//                    el.on("keydown", function(e, t, o){
//                        alert(e.getKey());
//                    }, el);
                }
            }
        });
        var editForm = new Ext.form.FormPanel({
            border : false,
            items : [ {
                xtype : "hidden",
                name : "id"
            }, {
                xtype : "textarea",
                name : "content",
                autoScroll : true,
                hideLabel : true,
                anchor : "100% 0"
            } ]
        });
        this.editorPanel = new Ext.Panel({
            flex: 1,
            border : true,
            bodyCssClass : "wiki_editor",
            autoScroll : true,
//            width: "100%",
            margins:'0 0 0 0',
            layout : "fit",
            tbar : [ {
                text : "Review",
                handler : this.reviewEdit,
                scope : this
            }, {
                text : "Submit",
                handler : this.submitEdit,
                scope : this
            } ],
            items : [ editForm ],
            hidden: this.power.hidden
        });
        // ! add(a).add(b)
        this.add([ this.contentPanel, this.editorPanel ]);
        var contentData = this.readContentData();
        this.setContentHtml(contentData);
        this.setEditorForm(contentData);
    },
    readContentData : function() {
        var contentData = "";
        var wartMsg = Ext.Msg.wait("", "Please Wait...");
        Ext.Ajax.request({
            url : this.getUrl,
            method : "post",
            async : false,
            scope : this,
            success : function(response, opts) {
                wartMsg.hide();
                contentData = Ext.decode(response.responseText).data;
            },
            failure : function(response, opts) {
                wartMsg.hide();
                // console.log('server-side failure with status code ' + responsetatus);
            }
        });
        return contentData;
    },
    setContentHtml : function(contentData) {
        var html = new GoogleCodeWikiParser().parse(contentData.content);
        if (this.contentPanel.body) {
            // same: this.contentPanel.body.dom.innerHTML = html;
            this.contentPanel.update(html);
        } else {
            this.contentPanel.html = html;
        }
    },
    setEditorForm : function(contentData) {
        this.editorPanel.getComponent(0).getForm().setValues(contentData);
    },
    reviewEdit : function() {
        var contentData = this.editorPanel.getComponent(0).getForm().getValues();
        this.setContentHtml(contentData);
    },
    submitEdit : function() {
        this.editorPanel.getComponent(0).getForm().submit({
            url : this.editUrl,
            scope : this,
            waitMsg : "",
            success : function(form, action) {
                Ext.Msg.alert("Tip", "Success!");
                this.reviewEdit();
            },
            failure : function(form, acition) {
                Ext.Msg.alert("Tip", "Failed!");
            }
        });
    }
});
